﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections.Generic;

public partial class ServiceStationManagement_Delivery : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("1000100");
        if (!IsPostBack)
        {
            BindDataList();
        }
    }
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    private void BindDataList()
    {
        StringBuilder strSql = new StringBuilder();
        if (Share.UserValue(0).ToLower() == "admin")
        {
            strSql.Append(" and orderdoc.byzd5='1' ");
        }
        else
        {
            strSql.Append(" and orderdoc.byzd5='1' and SS_admin='" + Share.UserValue(0) + "' ");
        }
        
        strSql.Append(txtOrderId.Text.Trim() == "" ? "" : " and ORDER_ID like '%" + txtOrderId.Text.Trim() + "%' ");
        strSql.Append(txtTranCode.Text.Trim() == "" ? "" : " and ORDER_TRANBILLCODE like '%" + txtTranCode.Text.Trim() + "%' ");
        strSql.Append(txtReceiver.Text.Trim() == "" ? "" : " and ORDER_GETER like '%" + txtReceiver.Text.Trim() + "%' ");
        strSql.Append(txtPhone1.Text.Trim() == "" ? "" : " and ORDER_GETERPHONE1 like '%" + txtPhone1.Text.Trim() + "%' ");
        strSql.Append(txtMobilePhone.Text.Trim() == "" ? "" : " and ORDER_GETERPHONE2 like '%" + txtMobilePhone.Text.Trim() + "%' ");
        strSql.Append(DropDownList1.SelectedValue == "-1" ? "" : " and BYZD6='" + DropDownList1.SelectedValue + "' ");
        strSql.Append(DropDownList2.SelectedValue == "-1" ? "" : " and BYZD7='" + DropDownList2.SelectedValue + "' ");

        int recordCount;
        DataSet dst = new BLL.ORDERDOC().GetDeliverOrder(GetStartIndex(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize), AspNetPager1.PageSize, strSql.ToString(), out recordCount);
        dlistDelivery.DataSource = dst;
        dlistDelivery.DataBind();
        AspNetPager1.RecordCount = recordCount;
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindDataList();
    }

    private int GetStartIndex(int pageIndex, int pageSize)
    {
        return 1 + (pageIndex - 1) * pageSize;
    }
    protected void dlistDelivery_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            CheckBox CheckBox1 = (CheckBox)e.Item.FindControl("CheckBox1");
            Button btnConfirmReceived = (Button)e.Item.FindControl("btnConfirmReceived");
            if (CheckBox1.Checked)
            {
                btnConfirmReceived.Enabled = false;
            }
        }
    }
    protected void dlistDelivery_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "received")
        {
            if (!Check.CheckPermission("1000100", "00014"))
            {
                Share.Alert(Page, "无权操作"); return;
            }
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update orderdoc set byzd7='1' where order_id='");
            strSql.Append(e.CommandArgument);
            strSql.Append("'|");

            char[] charToTrim = new char[] { '|' };
            string stringSql = strSql.ToString();
            string[] sqlCommandArray = stringSql.TrimEnd(charToTrim).Split(charToTrim);

            if (new SQLServerDAL.ORDERDOC().Deliver(sqlCommandArray))
            {
                Share.InsertLog("1000100", "00014");
                BindDataList();
            }
            else
            {
                Share.Alert(this.Page,"更新订单时出现错误，请重试！");
            }
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 1;
        BindDataList();
    }
}
